<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js">&quot;use strict&quot;;
Object.defineProperty(exports, &quot;__esModule&quot;, { value: true });
exports.UrlMatchValidator = void 0;
var regex_lib_1 = require(&quot;../regex-lib&quot;);
<span id='Autolinker-matcher-UrlMatchValidator'>/**
</span> * @private
 * @class Autolinker.matcher.UrlMatchValidator
 * @singleton
 *
 * Used by Autolinker to filter out false URL positives from the
 * {@link Autolinker.matcher.Url UrlMatcher}.
 *
 * Due to the limitations of regular expressions (including the missing feature
 * of look-behinds in JS regular expressions), we cannot always determine the
 * validity of a given match. This class applies a bit of additional logic to
 * filter out any false positives that have been matched by the
 * {@link Autolinker.matcher.Url UrlMatcher}.
 */
var UrlMatchValidator =  (function () {
    function UrlMatchValidator() {
    }
<span id='Autolinker-matcher-UrlMatchValidator-method-isValid'>    /**
</span>     * Determines if a given URL match found by the {@link Autolinker.matcher.Url UrlMatcher}
     * is valid. Will return `false` for:
     *
     * 1) URL matches which do not have at least have one period (&#39;.&#39;) in the
     *    domain name (effectively skipping over matches like &quot;abc:def&quot;).
     *    However, URL matches with a protocol will be allowed (ex: &#39;http://localhost&#39;)
     * 2) URL matches which do not have at least one word character in the
     *    domain name (effectively skipping over matches like &quot;git:1.0&quot;).
     *    However, URL matches with a protocol will be allowed (ex: &#39;intra-net://271219.76&#39;)
     * 3) A protocol-relative url match (a URL beginning with &#39;//&#39;) whose
     *    previous character is a word character (effectively skipping over
     *    strings like &quot;abc//google.com&quot;)
     *
     * Otherwise, returns `true`.
     *
     * @param {String} urlMatch The matched URL, if there was one. Will be an
     *   empty string if the match is not a URL match.
     * @param {String} protocolUrlMatch The match URL string for a protocol
     *   match. Ex: &#39;http://yahoo.com&#39;. This is used to match something like
     *   &#39;http://localhost&#39;, where we won&#39;t double check that the domain name
     *   has at least one &#39;.&#39; in it.
     * @return {Boolean} `true` if the match given is valid and should be
     *   processed, or `false` if the match is invalid and/or should just not be
     *   processed.
     */
    UrlMatchValidator.isValid = function (urlMatch, protocolUrlMatch) {
        if ((protocolUrlMatch &amp;&amp; !this.isValidUriScheme(protocolUrlMatch)) ||
            this.urlMatchDoesNotHaveProtocolOrDot(urlMatch, protocolUrlMatch) || // At least one period (&#39;.&#39;) must exist in the URL match for us to consider it an actual URL, *unless* it was a full protocol match (like &#39;http://localhost&#39;)
            (this.urlMatchDoesNotHaveAtLeastOneWordChar(urlMatch, protocolUrlMatch) &amp;&amp; // At least one letter character must exist in the domain name after a protocol match. Ex: skip over something like &quot;git:1.0&quot;
                !this.isValidIpAddress(urlMatch)) || // Except if it&#39;s an IP address
            this.containsMultipleDots(urlMatch)) {
            return false;
        }
        return true;
    };
    UrlMatchValidator.isValidIpAddress = function (uriSchemeMatch) {
        var newRegex = new RegExp(this.hasFullProtocolRegex.source + this.ipRegex.source);
        var uriScheme = uriSchemeMatch.match(newRegex);
        return uriScheme !== null;
    };
    UrlMatchValidator.containsMultipleDots = function (urlMatch) {
        var stringBeforeSlash = urlMatch;
        if (this.hasFullProtocolRegex.test(urlMatch)) {
            stringBeforeSlash = urlMatch.split(&#39;://&#39;)[1];
        }
        return stringBeforeSlash.split(&#39;/&#39;)[0].indexOf(&#39;..&#39;) &gt; -1;
    };
<span id='Autolinker-matcher-UrlMatchValidator-method-isValidUriScheme'>    /**
</span>     * Determines if the URI scheme is a valid scheme to be autolinked. Returns
     * `false` if the scheme is &#39;javascript:&#39; or &#39;vbscript:&#39;
     *
     * @private
     * @param {String} uriSchemeMatch The match URL string for a full URI scheme
     *   match. Ex: &#39;http://yahoo.com&#39; or &#39;mailto:a@a.com&#39;.
     * @return {Boolean} `true` if the scheme is a valid one, `false` otherwise.
     */
    UrlMatchValidator.isValidUriScheme = function (uriSchemeMatch) {
        var uriSchemeMatchArr = uriSchemeMatch.match(this.uriSchemeRegex), uriScheme = uriSchemeMatchArr &amp;&amp; uriSchemeMatchArr[0].toLowerCase();
        return uriScheme !== &#39;javascript:&#39; &amp;&amp; uriScheme !== &#39;vbscript:&#39;;
    };
<span id='Autolinker-matcher-UrlMatchValidator-method-urlMatchDoesNotHaveProtocolOrDot'>    /**
</span>     * Determines if a URL match does not have either:
     *
     * a) a full protocol (i.e. &#39;http://&#39;), or
     * b) at least one dot (&#39;.&#39;) in the domain name (for a non-full-protocol
     *    match).
     *
     * Either situation is considered an invalid URL (ex: &#39;git:d&#39; does not have
     * either the &#39;://&#39; part, or at least one dot in the domain name. If the
     * match was &#39;git:abc.com&#39;, we would consider this valid.)
     *
     * @private
     * @param {String} urlMatch The matched URL, if there was one. Will be an
     *   empty string if the match is not a URL match.
     * @param {String} protocolUrlMatch The match URL string for a protocol
     *   match. Ex: &#39;http://yahoo.com&#39;. This is used to match something like
     *   &#39;http://localhost&#39;, where we won&#39;t double check that the domain name
     *   has at least one &#39;.&#39; in it.
     * @return {Boolean} `true` if the URL match does not have a full protocol,
     *   or at least one dot (&#39;.&#39;) in a non-full-protocol match.
     */
    UrlMatchValidator.urlMatchDoesNotHaveProtocolOrDot = function (urlMatch, protocolUrlMatch) {
        return (!!urlMatch &amp;&amp;
            (!protocolUrlMatch || !this.hasFullProtocolRegex.test(protocolUrlMatch)) &amp;&amp;
            urlMatch.indexOf(&#39;.&#39;) === -1);
    };
<span id='Autolinker-matcher-UrlMatchValidator-method-urlMatchDoesNotHaveAtLeastOneWordChar'>    /**
</span>     * Determines if a URL match does not have either:
     *
     * a) a full protocol (i.e. &#39;http://&#39;), or
     * b) at least one word character after the protocol (i.e. in the domain name)
     *
     * At least one letter character must exist in the domain name after a
     * protocol match. Ex: skip over something like &quot;git:1.0&quot;
     *
     * @private
     * @param {String} urlMatch The matched URL, if there was one. Will be an
     *   empty string if the match is not a URL match.
     * @param {String} protocolUrlMatch The match URL string for a protocol
     *   match. Ex: &#39;http://yahoo.com&#39;. This is used to know whether or not we
     *   have a protocol in the URL string, in order to check for a word
     *   character after the protocol separator (&#39;:&#39;).
     * @return {Boolean} `true` if the URL match does not have a full protocol, or
     * at least one word character in it, `false` otherwise.
     */
    UrlMatchValidator.urlMatchDoesNotHaveAtLeastOneWordChar = function (urlMatch, protocolUrlMatch) {
        if (urlMatch &amp;&amp; protocolUrlMatch) {
            return (!this.hasFullProtocolRegex.test(protocolUrlMatch) &amp;&amp;
                !this.hasWordCharAfterProtocolRegex.test(urlMatch));
        }
        else {
            return false;
        }
    };
<span id='Autolinker-matcher-UrlMatchValidator-property-hasFullProtocolRegex'>    /**
</span>     * Regex to test for a full protocol, with the two trailing slashes. Ex: &#39;http://&#39;
     *
     * @private
     * @property {RegExp} hasFullProtocolRegex
     */
    UrlMatchValidator.hasFullProtocolRegex = /^[A-Za-z][-.+A-Za-z0-9]*:\/\//;
<span id='Autolinker-matcher-UrlMatchValidator-property-uriSchemeRegex'>    /**
</span>     * Regex to find the URI scheme, such as &#39;mailto:&#39;.
     *
     * This is used to filter out &#39;javascript:&#39; and &#39;vbscript:&#39; schemes.
     *
     * @private
     * @property {RegExp} uriSchemeRegex
     */
    UrlMatchValidator.uriSchemeRegex = /^[A-Za-z][-.+A-Za-z0-9]*:/;
<span id='Autolinker-matcher-UrlMatchValidator-property-hasWordCharAfterProtocolRegex'>    /**
</span>     * Regex to determine if at least one word char exists after the protocol (i.e. after the &#39;:&#39;)
     *
     * @private
     * @property {RegExp} hasWordCharAfterProtocolRegex
     */
    UrlMatchValidator.hasWordCharAfterProtocolRegex = new RegExp(&#39;:[^\\s]*?[&#39; + regex_lib_1.alphaCharsStr + &#39;]&#39;);
<span id='Autolinker-matcher-UrlMatchValidator-property-ipRegex'>    /**
</span>     * Regex to determine if the string is a valid IP address
     *
     * @private
     * @property {RegExp} ipRegex
     */
    UrlMatchValidator.ipRegex = /[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?(:[0-9]*)?\/?$/;
    return UrlMatchValidator;
}());
exports.UrlMatchValidator = UrlMatchValidator;
//# sourceMappingURL=url-match-validator.js.map</pre>
</body>
</html>
